	/**开市时间长 小时**/
	var mt_hour=20;
	/**历史**/
	var hisfsList=[],hisfk5List=[],hisfk15List=[],hisfk30List=[],hisfk60List=[],hisfkdayList=[];
	/**当天**/
	var todayfsList=[],todayfk5List=[],todayfk15List=[],todayfk30List=[],todayfk60List=[],todaykdayList=[];
	
	/**历史分时**/
	var nameFsTitleList = ["sn", "datetime", "bid", "wid", "close", "h", "l"]; //所需要的key数组	
	var nameFsList = ["t", "c", "m", "a", "hd"]; //所需要的key数组
	
	/**历史分K**/
	var nameFkTitleList = ["sn", "datetime", "bid", "wid", "k"]; //所需要的key数组	
	var nameFkList = ["t", "o", "h", "l", "c","m","a","hd"]; //所需要的key数组
	
	/** 盘面基本数据**/
	var nameQuotesList = ["wid","b1", "ba1", "s1", "sa1", "n","o","h","l","c","name","d","t"]; //所需要的key数组
	/** 盘面基本数据(量)**/
	var nameQuotes2List = ["title1","wid","oid", "type", "point", "sa", "nid","name","title2"]; //所需要的key数组
	
	var fsPoint=60;
	var fk5Point=30;
	var fk15Point=30;
	var fk30Point=30;
	var fk60Point=30;

 	var curType=1,
 	    Ktimer,
 	    Btimer,
 	    
 	    isShow=false,
 	    isInit=false,
 	    option,
     	chart1Ct,
		chart2Ct,
     	chart1,
     	chart2,
     	charts1Rendered=false,
     	charts2Rendered=false,cfg={},
     	
     	todayfk5=[],
     	todayfk15=[],
     	todayfk30=[],
     	todayfk60=[],
     	todayfkday=[];
     	
 
 	function getCurType(){return curType;};
 	
 	function doSwitch(type)
 	{
        	curType=type;
        	chart1Ct[type==1?'show':'hide']();
        	chart2Ct[type!=1?'show':'hide']();

            $(".time td a").removeClass('selected');
            this.loadData(type);
            $(".time td a[data=" + type + "]").addClass("selected");
            
     }
 	
 	function loadData(type){
        	var charts=this;
        	//console.log(type);
        	switch(type)
        	{
        	case 1:  //取分时历史
        		var hisfs={};
        		hisfs.bid=cfg.bid;
        		hisfs.wid=cfg.wid;
        		getTodayfs(hisfs);
          		break;
        		
        	case 100:    //日K线
        		var hisfkday={};
        		hisfkday.bid=cfg.bid;
        		hisfkday.wid=cfg.wid;
        		hisfkday.k=type;
        		getHisdayfk(hisfkday);
        		
/*        		
        		for(var i in hisfkdayList)
        		{
        			if(hisfkdayList[i].bid==cfg.bid && hisfkdayList[i].wid==cfg.wid)
        			{
        				hisfkday=hisfkdayList[i];
        				break;
        			}
        		}
        		if(hisfkday.items==undefined || hisfkday.items.length==0)
        		{
         			getHisdayfk(hisfkday);
        		}
        		else
        			getTodaydayfk(hisfkday);
  */
        		break;
        	case 5:
        		var hisfk5={};
        		hisfk5.bid=cfg.bid;
        		hisfk5.wid=cfg.wid;
        		hisfk5.k=type;
        		getTodayfk(hisfk5);
        		break;
        	case 15:
        		var hisfk15={};
        		hisfk15.bid=cfg.bid;
        		hisfk15.wid=cfg.wid;
        		hisfk15.k=type;
        		getTodayfk(hisfk15);
        		break;        		
          	case 30:
         		var hisfk30={};
        		hisfk30.bid=cfg.bid;
        		hisfk30.wid=cfg.wid;
        		hisfk30.k=type;
        		getTodayfk(hisfk30);
        		break;
          	case 60:
        		var hisfk60={};
        		hisfk60.bid=cfg.bid;
        		hisfk60.wid=cfg.wid;
        		hisfk60.k=type;
        		getTodayfk(hisfk60);
        		break;        		
        	
        	}
        }
        
 	function renderData(type,data){
            var precision=data.mindecimal ? data.mindecimal : 1 ,
                format=function (v) {
	                return  data.mindecimal?v.toFixed(data.mindecimal):parseInt(v);
	             },
	             x;
            if(type==1){
            	x=12+(data.data[0]+'').length*7;
            	//console.log("x="+x);
           	 	option={
           	        color : ['#1BBCDC'],
           	        backgroundColor: null,
           	        tooltip: {
           	            trigger: 'axis',
           	            showDelay: 0             // 显示延迟，添加显示延迟可以避免频繁切换，单位ms
           	        },
           	        legend: {
           	            y: -30,
           	            data: ['金诚指数', '最新价格', '虚拟数据']
           	        },
           	        grid: {
           	            x: x,
           	            y: 10,
           	            x2: 10,
           	            y2: 30,
           	            backgroundColor: 'rgba(0,0,0,0)',
           	            borderWidth: 0,
           	            borderColor: 'rgba(0,0,0,0)'
           	        },
           	        dataZoom: {
           	            y: 250,
           	            show: false,
           	            realtime: true,
           	            start: 0,
           	            end: 100

           	        },
           	        xAxis: [
           	            {
           	                axisLine: {// 轴线
           	                    show: true,
           	                    lineStyle: {
           	                        color: '#FFF',
           	                        width: 2
           	                    }
           	                },
           	                type: 'category',
           	                boundaryGap: true,
           	                axisTick: {show: false},
           	                splitLine: {show: false},
           	                axisLabel: {show: true},
           	                data: data.time
           	            }
           	        ],
           	        yAxis: [
           	            {
           	                type: 'value',
           	                axisLine: {
           	                    show: true,
           	                    lineStyle: {
           	                        color: '#FFF',
           	                        width: 2
           	                    }
           	                },
           	                scale: '_max',
           	                scaleSize: 5,
           	                precision: precision,
           	                boundaryGap: true,
           	                splitArea: {
           	                    show: false
           	                }
           	                ,
           	                axisLabel: {
           	                    formatter: format
           	                }


           	            }
           	        ],
           	        series: [
           	            {
           	                name: '最新价格',
           	                type: 'line',
           	                symbol: 'none',
           	            smooth:true,
           	            itemStyle: {normal: {areaStyle: {type: 'default'}}},
           	                data:data.data,
           	                precision: 2 
           	            }
           	        ]
           	    };
           if(!charts1Rendered){
             	chart1.setOption(option);
             	charts1Rendered=true;
            }else{
            	chart1.dispose();
            	chart1=echarts.init(chart1Ct.get(0));
            	chart1.setOption(option);
            }
        }else{
        	//console.log("画K0");
       	 	option ={
   		        grid: {
   		            x: 50,
   		            y: 10,
   		            x2: 0,
   		            y2: 30,
   		            backgroundColor: 'rgba(0,0,0,0)',
   		            borderWidth: 0,
   		            borderColor: 'rgba(0,0,0,0)'
   		        },
   		        tooltip: {
   		            trigger: 'axis',
   		            axisPointer: {
   		                type: 'line'
   		            },
   		            formatter: function(params, ticket, callback) {
   		                var f = params[0].name;
   		                f += '<br/>  开市 : ' + params[0].data[0] + '  最高 : ' + params[0].data[3];
   		                f += '<br/>  收市 : ' + params[0].data[1] + '  最低 : ' + params[0].data[2];
   		                return f;
   		            }
   		        },
   		        xAxis: {
   		            type: 'category',
   		            data: data.time,
   		            scale: true,
   		            boundaryGap: true,//['10%','10%'],
   		            splitNumber: 20,
   		            axisLine: {// 轴线
   		                show: false,
   		                onZero:true,
   		                lineStyle: {
   		                    color: '#fff',
   		                    width: 2
   		                }
   		            }
   		        },
   		        yAxis: {
   		            type: 'value',
   		            axisLine: {
   		                show: false,
   		                lineStyle: {
   		                    color: '#fff',
   		                    width: 2
   		                }
   		            },
   		            scale: '_max',
   		            precision: precision,
   		            boundaryGap: [0.001, 0.001],
   		            splitArea: {
   		                show: false
   		            },
   		            axisLabel: {
   		                formatter: format
   		            },
   		            splitArea: {
   		                show: false
   		            },
   		            scaleSize: 5
   		        },
   		        series: [
   		            {
   		                type: 'candlestick',
   		                data: data.data,
   		                itemStyle: {
   		                    normal: {
   		                        color: '#d87a80', // 阳线填充颜色
   		                        color0: '#2ec7c9', // 阴线填充颜色
   		                        borderColor: '#d87a80',
   		                        borderColor0: '#2ec7c9'

       		                    }
       		                }
       		            }
       		        ]
       		 };
           	 if(!charts2Rendered){
             	chart2.setOption(option);
             	charts2Rendered=true;
            }else{
            	chart2.dispose();
            	chart2=echarts.init(chart2Ct.get(0));
            	chart2.setOption(option);
            }
            }
    	}
 	
   		var ct=$('#qz-Tab'),
    	that=this;
    	ct.on('click','li',function(){
    		var type=$(this).attr('data');
    		that.doSwitch(type);
    	});
    	
    	
    	function event(){
    		var ct=$('#qz-Tab'),
    		    that=this;
    		ct.on('click','li',function(){
    		    var type=$(this).attr('data');
    		    that.doSwitch(type*1);
    		});
    	}
    	
 /*   	
   		var ct=$('#charts_nav'),
    	that=this;
    	ct.on('click','a',function(){
    		var type=$(this).attr('data');
    		that.doSwitch(type);
    	})

    	function event(){
    		var ct=$('#charts_nav'),
    		    that=this;
    		ct.on('click','a',function(){
    		    var type=$(this).attr('data');
    		    that.doSwitch(type);
    		});
    	}
*/    	    	
    	
    	function chartsinit(cfgObj){
    		cfg=cfgObj;
     		if(!isInit){
	    		chart1Ct=$('#charts1'),
			    chart2Ct=$('#charts2'),
	     	    chart1=echarts.init(chart1Ct.get(0)),
	     	    chart2=echarts.init(chart2Ct.get(0)),
	     	    this.doSwitch(1);
	     	    this.event();
	     	   loadDataBase();
	     	   loadDataBase2();
	     	    isInit=true;
    		}
    		else
    		{
    			this.loadData(getCurType());
    			clearTimer();
    			loadDataBase();
    			loadDataBase2();
    			
    		}
     		
      		isShow=true;
   
    		
    		//分时数据定时刷新
    		Ktimer=setTimeout(function(){
			/*	
			 	if(getCurType()==1){
					loadData(1);
				}
			*/
				loadData(getCurType());
				
				if(isShow)Ktimer=setTimeout(arguments.callee,15000);
			},15000);
			
    		//盘面数据定时刷新
    		Btimer=setTimeout(function(){
				loadDataBase();
				loadDataBase2();
				if(isShow)Btimer=setTimeout(arguments.callee,3000);
			},2000);
			
    	}
    	
    	function clearTimer(){
    		clearTimeout(Ktimer);
    		clearTimeout(Btimer);
     		isShow=false;
    	}

    	
    	function loadDataBase(){
    		var bidVal=cfg.bid,widVal=cfg.wid;
    		//行情列表
    	    $.ajax({
    	        url: "/api/v2/market/list",
    	        data: {bid:bidVal,wid:widVal,f:"wl1nohlcn1dt"},
                type: 'post',
                dataType: 'text',
                contentType:"application/x-www-form-urlencoded;charset=utf-8",
                beforeSend: function () {
                },
                success: function (resp) {
                	//console.log("行情列表");
                	//console.log(resp);
                	var valueList = [];
                	resp.split("\n").forEach(function(item){
                		if(item == "" || item == null){
                			return;
                		}
                		item = item.replace(";",",");
                		
                		item = item.replace(/(\s*[;；]\s*)/g, ",");
                		item = item.replace(/[;；]$/, "");
                		
                		item = item.replace(/(\s*[,，]\s*)/g, ",");
                		item = item.replace(/[,，]$/, "");
                		valueList.push(item.split(","));
                	});
                	var quotesTmp=transferList(null,nameQuotesList, valueList);
                	//console.log("即时行情");
                	//console.log(quotesTmp);
                	quotesTmp=quotesTmp.items;
                	messHide();
                	
    	            if(quotesTmp!=undefined && quotesTmp!=null && quotesTmp.length>0){
    	            	quotes=[];//重置
   	        		    for(var k in quotesTmp)
	        		    {	
   	        		    	var tmp=quotesTmp[k];
   	        		    	for(var i in vrGoodsList)
   	        		    	{
   	        		    		var obj=vrGoodsList[i];
 	    	        		    if(tmp.wid==(obj.bid+obj.wid))
	    	        		    {
 	    	        		    	var tmpObj=tmp;
 	    	        		    	tmpObj.bid=obj.bid;
 	    	        		    	tmpObj.wid=obj.wid;
	    	        		    	quotes.push(tmpObj);//往数组中放属性
	    	        		        break;
	    	        		    }
    	        		    }
    	        		          
    	        		}
   	        		    
   	        		 refreshQuoteUI(quotes);
   	        		
    	           }
    	           else
    	           {
    	        	   messShow("无行情数据----");
    	           }
    		    	
    		    },
    		    error:function(xhr,status){
    		    	messShow("网络忙，请重试");
    		    }
    		});
          
        }
    	
    	
	function loadDataBase2(){
		var widVal=cfg.wid;
		//行情列表2
	    $.ajax({
	        url: "/api/v2/market/marketext",
	        data: {wid:widVal,pid:"6806"},
            type: 'post',
            dataType: 'text',
            contentType:"application/x-www-form-urlencoded;charset=utf-8",
            beforeSend: function () {
            },
            success: function (resp) {
            	//console.log("行情列表2");
            	//console.log(resp);
            	var valueList = [];
            	resp.split("\n").forEach(function(item){
            		if(item == "" || item == null || item == "6806"){
            			return;
            		}
            		item = item.replace(";",",");
            		
            		item = item.replace(/(\s*[;；]\s*)/g, ",");
            		item = item.replace(/[;；]$/, "");
            		
            		item = item.replace(/(\s*[,，]\s*)/g, ",");
            		item = item.replace(/[,，]$/, "");
             		
            		valueList.push(item.split("|"));
            	});
            	//console.log(valueList);
            	var quotesTmp=transferList(null,nameQuotes2List, valueList);
            	//console.log("即时行情2");
            	//console.log(quotesTmp);
            	quotesTmp=quotesTmp.items;
            	messHide();
	           if(quotesTmp!=undefined && quotesTmp!=null && quotesTmp.length>0){
   
	     		    for(var k in quotesTmp)
        		    {	
          	    		if(quotesTmp[k].wid==cfg.wid && quotesTmp[k].type=="B")
        	    		{
			     	    	$('#amount1').html(getMrInt(quotesTmp[k].sa));
        	    		}
           	    		if(quotesTmp[k].wid==cfg.wid && quotesTmp[k].type=="S" )
        	    		{
			    	    	$('#amount2').html(getMrInt(quotesTmp[k].sa));
        	    		}
  	        		          
	        		}
	        	   
	           }
	           else
	           {
	        	   messShow("无行情数据----");
	           }
		    	
		    },
		    error:function(xhr,status){
		    	messShow("网络忙，请重试");
		    }
		});
      
    }   	
    	
    	
    	
    function refreshQuoteUI(quotes)
    {
    	
    	$.each(quotes, function(index, objVal) {
    		if(objVal.bid==cfg.bid && objVal.wid==cfg.wid)
    		{
    			//console.log("解析后：");
            	//console.log(quotes);
            	
    			//var fixedNumber=Math.pow(10,objVal.mindecimal);
    	    	$('#curChartTime').html(objVal.time);
    	    	
    	    	$('#curChartPrice').removeClass();
    	    	if(objVal.c*1.0<objVal.o*1.0)
    	    	{
    	    		$('#curChartPrice').addClass('cgreen');
    	    	}
    	    	else
    	    	{
    	    		$('#curChartPrice').addClass('cred');
    	    	}
    	    	$('#curChartPrice').html(objVal.c);
    	    	
    	    	$('#openChartPrice').html(objVal.o);
    	    	
    	    	$('#highChartPrice').removeClass();
    	    	if(objVal.h*1.0<objVal.o*1.0)
    	    	{
    	    		$('#highChartPrice').addClass('cgreen');
    	    	}
    	    	else
    	    	{
    	    		$('#highChartPrice').addClass('cred');
    	    	}
    	    	$('#highChartPrice').html(objVal.h);
    	    	
    	    	$('#lowChartPrice').removeClass();
    	    	if(objVal.l*1.0<objVal.o*1.0)
    	    	{
    	    		$('#lowChartPrice').addClass('cgreen');
    	    	}
    	    	else
    	    	{
    	    		$('#lowChartPrice').addClass('cred');
    	    	}
    	    	$('#lowChartPrice').html(objVal.l);
    	    	
    	    	$('#nowprice1').html(objVal.b1);
    	    	$('#nowprice2').html(objVal.s1);
    	    	//$('#amount1').html(objVal.ba1);
    	    	//$('#amount2').html(objVal.sa1);
    	        var directValue  = $('input[name="direct"]:checked').val();
    	    	var priceVal="";
    	    	if(directValue=="B")
    	    	{
    	    		$('#price').val(objVal.s1);
    	    	}
    	    	else
    	    	{
    	    		$('#price').val(objVal.b1);
    	    	}
    	    	UpdateBuyRmb();
    	    	
    	    	if(objVal.o*1.0==0 && objVal.c*1.0==0)
    	    	{
    	    		messShow("今天停市----");
    	    	}
    	    	
    		}
    	});
    	
    	createHtmlForOpenOrder();
    	
    	


    }
    

    /**取前一交易日历史分时**/
    function getHisfs(todayfs)
    {
    	var bidVal=todayfs.bid,widVal=todayfs.wid;
    	var tmp=null;
    	var dd=$.ajax({
            url: "/api/v2/market/hisfs",
            data: {bid:bidVal,wid:widVal},
            type: 'post',
            dataType: 'text',
            contentType:"application/x-www-form-urlencoded;charset=utf-8",
            beforeSend: function () {
            },
            success: function (resp) {
            	var valueList = [];
            	resp.split("\n").forEach(function(item){
            		if(item == "" || item == null){
            			return;
            		}
            		item = item.replace(/(\s*[,，]\s*)/g, ",");
            		item = item.replace(/[,，]$/, "");
            		valueList.push(item.split(","));
            	});
            	tmp=transferList(nameFsTitleList,nameFsList, valueList);
            	//console.log("历史分时0");
              	//console.log(tmp);
              	//加入记录
              	hisfsList.push(tmp);
            	//去绘制
			  	
             },
            error: function (data) {
            	//messShow("无行情数据----");
            },
            complete: function () {
            	drawfs(todayfs,tmp);
            }
        });

    }
 
    /** 当天分时 **/
    function getTodayfs(hisfs)
    {
    	var bidVal=hisfs.bid,widVal=hisfs.wid;
    	var idxVal="-1";
    	//取当天的idx
    	var todayfs={};
		for(var i in todayfsList)
		{
			if(todayfsList[i].bid==bidVal && todayfsList[i].wid==widVal)
			{
				todayfs=todayfsList[i];
				break;
			}
		}
    	
    	if(todayfs.items!=undefined && todayfs.items.length>0)
    	{
    		idxVal=""+(todayfs.length-1);
    	}
    	
    	$.ajax({
            url: "/api/v1/market/fs",
            data: {bid:bidVal,wid:widVal,idx:idxVal},
            type: 'post',
            dataType: 'json',
            contentType:"application/x-www-form-urlencoded;charset=utf-8",
            beforeSend: function () {
            },
            success: function (data) {
            	
            	//console.log("当天分时");
            	//console.log(data);
            	var todayfs={};
            	
            	if(data.items==undefined || data.items.length==0)  //当天不开市
            	{
            		todayfs.items=[];
                  	todayfs.bid=data.bid;
                	todayfs.wid=data.wid;
                	todayfs.h=data.h;
                	todayfs.l=data.l;
                	todayfs.y=data.y;
                	
                	todayfsList.push(todayfs);
            	}
            	else
            	{
            		//取出原存有todayfs数据
            		for(var i in todayfsList)
            		{
            			if(todayfsList[i].bid==data.bid && todayfsList[i].wid==data.wid)
            			{
            				todayfs=todayfsList[i];
            				break;
            			}
            		}
              		
            		if(todayfs.items==undefined || todayfs.items.length==0 )
                	{
                			todayfs=data;
                	}
                	else
                	{
                      	todayfs.bid=data.bid;
                    	todayfs.wid=data.wid;
                    	todayfs.h=data.h;
                    	todayfs.l=data.l;
                    	todayfs.y=data.y;
                    	
                    	if(data.items.length>0)
                    	{
                    		todayfs.items.pop();//移除最后一个元素
                    		todayfs.items=todayfs.items.concat(data.items);
                    	}
                	}
            		//更新todayfsList
               		for(var i in todayfsList)
            		{
            			if(todayfsList[i].bid==data.bid && todayfsList[i].wid==data.wid)
            			{
            				todayfsList[i]=todayfs;
            				break;
            			}
            		}
            		
            	}
            	
            	if(todayfs.items.length>=fsPoint)
            	{
            		drawfs(todayfs,null); //去绘图
            	}
            	else
            	{
            		//取历史
                	var hisfs={};
                	hisfs.bid=data.bid;
                	hisfs.wid=data.wid;
            		for(var i in hisfsList)
            		{
            			if(hisfsList[i].bid==data.bid && hisfsList[i].wid==data.wid)
            			{
            				hisfs=hisfsList[i];
            				break;
            			}
            		}
            		if(hisfs.items==undefined || hisfs.items.length==0)//历史不存在
            		{
            			getHisfs(todayfs); //去取历史，然后再绘图
            		}
            		else //已历史存在
            		{
            			drawfs(todayfs,hisfs);//去绘图
            		}
            	}
             },
            error: function (data) {
                showToast('网络忙，请重试');
            },
            complete: function () {
            }
        });    
    }
    
    
    function drawfs(todayfs,hisfs)
    {
    	var temp={
                   data:[],
                   time:[]
               };
    	/**http://www.cnblogs.com/xingmeng/p/4081225.html**/
 	
    	var datalist=null;
		
    	if(todayfs.items.length<fsPoint && hisfs!=null && hisfs.items!=undefined && hisfs.items.length>1)
    	{
    		var pos=(hisfs.items.length-1)-(fsPoint-todayfs.items.length); //hisfs[0]为数据头 hisfs[1]开始才为明细数据
    		if(pos<1) pos=1; //hisfs[0]不是数据明细
    		datalist=hisfs.items.slice(pos).concat(todayfs.items);
    	}
    	else
    	{
    		var pos=todayfs.items.length-fsPoint;
    		if(pos<0) pos=0;
    		datalist=todayfs.items.slice(pos);
    	}
    	if(datalist!=null)
    	{
       		var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/ ;
    		for(var i = 0; i < datalist.length; i++){
    			temp.data.push(datalist[i].c);
    			var t=datalist[i].t;
    			var formatedDate = t.replace(pattern, '$1-$2-$3 $4:$5');
     			var d=new Date(formatedDate.replace(/-/g, "/"));
    			temp.time.push(d.format('hh:mm'));
    		}
    	}
    	console.log(temp);
    	renderData(1,temp);
    	
    }
    
    /**历史分K**/
    function getHisfk(todayfk,idxVal,fkPoint)
    {
    	var tmp=null;
    	var dd=$.ajax({
            url: "/api/v2/market/hisfk",
            data: {bid:todayfk.bid,wid:todayfk.wid,period:todayfk.period},
            type: 'post',
            dataType: 'text',
            contentType:"application/x-www-form-urlencoded;charset=utf-8",
            beforeSend: function () {
            },
            success: function (resp) {
            	//console.log("历史分K0");
            	//console.log(resp);
            	var valueList = [];
            	resp.split("\n").forEach(function(item){
            		if(item == "" || item == null){
            			return;
            		}
            		item = item.replace(/(\s*[,，]\s*)/g, ",");
            		item = item.replace(/[,，]$/, "");
            		valueList.push(item.split(","));
            	});
            	var tmp=transferList(nameFkTitleList,nameFkList, valueList);
            	//console.log(tmp);
               	//加入记录
            	switch(tmp.k*1)
            	{
            	case 5:
            		hisfk5List.push(tmp);
            		break;
              	case 15:
            		hisfk15List.push(tmp);
            		break;
              	case 30:
            		hisfk30List.push(tmp);
            		break;
              	case 60:
            		hisfk60List.push(tmp);
            		break;
            	}
            	drawfk(todayfk,tmp,fkPoint);  //去绘图
			  	
             },
            error: function (data) {
            	tmp=null;
                //showToast('网络忙，请重试');
            },
            complete: function () {
            	drawfk(todayfk,tmp,fkPoint);  //去绘图
            }
        });

    }
    /** 当天分K **/
    function getTodayfk(hisfk)
    {
    	var bidVal=hisfk.bid,widVal=hisfk.wid;
    	var idxVal="1";
    	var periodVal="5"; //5分钟分K
    	var type=hisfk.k*1.0;
    	var todayfk={};
    	if(type==5)
    	{
    		periodVal="5"; //5分钟分K
    		for(var i in todayfk5List)
    		{
    			if(todayfk5List[i].bid==bidVal && todayfk5List[i].wid==widVal)
    			{
    				todayfk=todayfk5List[i];
    				break;
    			}
    		}
    	}
    	else if(type==15)
    	{
    		periodVal="15"; //15分钟分K
    		for(var i in todayfk15List)
    		{
    			if(todayfk15List[i].bid==bidVal && todayfk15List[i].wid==widVal)
    			{
    				todayfk=todayfk15List[i];
    				break;
    			}
    		}
    	}
    	else if(type==30)
    	{
    		periodVal="30"; //30分钟分K
    		for(var i in todayfk30List)
    		{
    			if(todayfk30List[i].bid==bidVal && todayfk30List[i].wid==widVal)
    			{
    				todayfk=todayfk30List[i];
    				break;
    			}
    		}
    	}
    	else if(type==60)
    	{
    		periodVal="60"; //60分钟分K
    		for(var i in todayfk60List)
    		{
    			if(todayfk60List[i].bid==bidVal && todayfk60List[i].wid==widVal)
    			{
    				todayfk=todayfk60List[i];
    				break;
    			}
    		}
    	}
		
   		if(todayfk.items!=undefined && todayfk.items.length>0)
    	{
    		idxVal=""+(todayfk.items.length);
    	}
    	
    	$.ajax({
            url: "/api/v1/market/fk",
            data: {bid:bidVal,wid:widVal,period:periodVal,idx:idxVal},
            type: 'post',
            dataType: 'json',
            contentType:"application/x-www-form-urlencoded;charset=utf-8",
            beforeSend: function () {
            },
            success: function (data) {
            	//console.log("当天分K");
            	//console.log(data);
          	
            	var todayfk={};
            	todayfk.bid=data.bid;
            	todayfk.wid=data.wid;
            	todayfk.period=data.period;
            	var hisfk={};
            	var fkPoint=0;
            	
            	if(data.period==5)
            	{
            		fkPoint=fk5Point;
             		//得到旧的todayfk
              		for(var i in todayfk5List)
            		{
            			if(todayfk5List[i].bid==data.bid && todayfk5List[i].wid==data.wid)
            			{
            				todayfk=todayfk5List[i];
            				break;
            			}
            		}
              		//组成新的todayfk
                  	if(todayfk.items==undefined)
                	{
                  		todayfk=data;
                  		if(todayfk.items==null)
                  		{
                  			todayfk.items=[];
                  		}
                	}
                	else
                	{
                	
                    	if(data.items!=null && data.items.length>0)
                    	{
                    		todayfk.items.pop();//移除最后一个元素
                    		todayfk.items=todayfk.items.concat(data.items);
                    	}
                    		
                	}
                  	//更新到 todayfk5List
              		for(var i in todayfk5List)
            		{
            			if(todayfk5List[i].bid==data.bid && todayfk5List[i].wid==data.wid)
            			{
            				todayfk5List[i]=todayfk;
            				break;
            			}
            		}
              		
              		if(todayfk.items.length>=fkPoint)  //数据足够
              		{
              			drawfk(todayfk,null,fkPoint);  //直接绘图
              		}
              		else //数据不足
              		{
                		//得到hisfk
                		for(var i in hisfk5List)
                		{
                			if(hisfk5List[i].bid==todayfk.bid && hisfk5List[i].wid==todayfk.wid)
                			{
                				hisfk=hisfk5List[i];
                				break;
                			}
                		}
                		if(hisfk.items==undefined || hisfk.items.length==0) //历史不存
                		{
                			var pos=fkPoint-todayfk.items.length;
                			var idx=Math.ceil(pos/(mt_hour*60/todayfk.period));
                			getHisfk(todayfk,idx,fkPoint); //去取历史，然后绘图
                		}
                		else  //历史已存在
                		{
                			drawfk(todayfk,hisfk,fkPoint);  //直接绘图
                		}
              		}
            	}
            	else if(data.period==15)
            	{
            		fkPoint=fk15Point;
             		//得到旧的todayfk
              		for(var i in todayfk15List)
            		{
            			if(todayfk15List[i].bid==data.bid && todayfk15List[i].wid==data.wid)
            			{
            				todayfk=todayfk15List[i];
            				break;
            			}
            		}
              		//组成新的todayfk
                  	if(todayfk.items==undefined)
                	{
                  		todayfk=data;
                  		if(todayfk.items==null)
                  		{
                  			todayfk.items=[];
                  		}
                	}
                	else
                	{
                	
                    	if(data.items!=null && data.items.length>0)
                    	{
                    		todayfk.items.pop();//移除最后一个元素
                    		todayfk.items=todayfk.items.concat(data.items);
                    	}
                    		
                	}
                  	//更新到 todayfk15List
              		for(var i in todayfk15List)
            		{
            			if(todayfk15List[i].bid==data.bid && todayfk15List[i].wid==data.wid)
            			{
            				todayfk15List[i]=todayfk;
            				break;
            			}
            		}
              		
              		if(todayfk.items.length>=fkPoint)  //数据足够
              		{
              			drawfk(todayfk,null,fkPoint);  //直接绘图
              		}
              		else //数据不足
              		{
                		//得到hisfk
                		for(var i in hisfk15List)
                		{
                			if(hisfk15List[i].bid==todayfk.bid && hisfk15List[i].wid==todayfk.wid)
                			{
                				hisfk=hisfk15List[i];
                				break;
                			}
                		}
                		if(hisfk.items==undefined || hisfk.items.length==0) //历史不存
                		{
                			var pos=fkPoint-todayfk.items.length;
                			var idx=Math.ceil(pos/(mt_hour*60/todayfk.period));
                			getHisfk(todayfk,idx,fkPoint); //去取历史，然后绘图
                		}
                		else  //历史已存在
                		{
                			drawfk(todayfk,hisfk,fkPoint);  //直接绘图
                		}
              		}
            	}            	
            	else if(data.period==30)
            	{
            		fkPoint=fk30Point;
             		//得到旧的todayfk
              		for(var i in todayfk30List)
            		{
            			if(todayfk30List[i].bid==data.bid && todayfk30List[i].wid==data.wid)
            			{
            				todayfk=todayfk30List[i];
            				break;
            			}
            		}
              		//组成新的todayfk
                  	if(todayfk.items==undefined)
                	{
                  		todayfk=data;
                  		if(todayfk.items==null)
                  		{
                  			todayfk.items=[];
                  		}
                	}
                	else
                	{
                	
                    	if(data.items!=null && data.items.length>0)
                    	{
                    		todayfk.items.pop();//移除最后一个元素
                    		todayfk.items=todayfk.items.concat(data.items);
                    	}
                    		
                	}
                  	//更新到 todayfk30List
              		for(var i in todayfk30List)
            		{
            			if(todayfk30List[i].bid==data.bid && todayfk30List[i].wid==data.wid)
            			{
            				todayfk30List[i]=todayfk;
            				break;
            			}
            		}
              		
              		if(todayfk.items.length>=fkPoint)  //数据足够
              		{
              			drawfk(todayfk,null,fkPoint);  //直接绘图
              		}
              		else //数据不足
              		{
                		//得到hisfk
                		for(var i in hisfk30List)
                		{
                			if(hisfk30List[i].bid==todayfk.bid && hisfk30List[i].wid==todayfk.wid)
                			{
                				hisfk=hisfk30List[i];
                				break;
                			}
                		}
                		if(hisfk.items==undefined || hisfk.items.length==0) //历史不存
                		{
                			var pos=fkPoint-todayfk.items.length;
                			var idx=Math.ceil(pos/(mt_hour*60/todayfk.period));
                			getHisfk(todayfk,idx,fkPoint); //去取历史，然后绘图
                		}
                		else  //历史已存在
                		{
                			drawfk(todayfk,hisfk,fkPoint);  //直接绘图
                		}
              		}
            	}            	
            	else if(data.period==60)
            	{
               		fkPoint=fk60Point;
             		//得到旧的todayfk
              		for(var i in todayfk60List)
            		{
            			if(todayfk60List[i].bid==data.bid && todayfk60List[i].wid==data.wid)
            			{
            				todayfk=todayfk60List[i];
            				break;
            			}
            		}
              		//组成新的todayfk
                  	if(todayfk.items==undefined)
                	{
                  		todayfk=data;
                  		if(todayfk.items==null)
                  		{
                  			todayfk.items=[];
                  		}
                	}
                	else
                	{
                	
                    	if(data.items!=null && data.items.length>0)
                    	{
                    		todayfk.items.pop();//移除最后一个元素
                    		todayfk.items=todayfk.items.concat(data.items);
                    	}
                    		
                	}
                  	//更新到 todayfk60List
              		for(var i in todayfk60List)
            		{
            			if(todayfk60List[i].bid==data.bid && todayfk60List[i].wid==data.wid)
            			{
            				todayfk60List[i]=todayfk;
            				break;
            			}
            		}
              		
              		if(todayfk.items.length>=fkPoint)  //数据足够
              		{
              			drawfk(todayfk,null,fkPoint);  //直接绘图
              		}
              		else //数据不足
              		{
                		//得到hisfk
                		for(var i in hisfk60List)
                		{
                			if(hisfk60List[i].bid==todayfk.bid && hisfk60List[i].wid==todayfk.wid)
                			{
                				hisfk=hisfk60List[i];
                				break;
                			}
                		}
                		if(hisfk.items==undefined || hisfk.items.length==0) //历史不存
                		{
                			var pos=fkPoint-todayfk.items.length;
                			var idx=Math.ceil(pos/(mt_hour*60/todayfk.period));
                			getHisfk(todayfk,idx,fkPoint); //去取历史，然后绘图
                		}
                		else  //历史已存在
                		{
                			drawfk(todayfk,hisfk,fkPoint);  //直接绘图
                		}
              		}
            	}          
            	
  
             },
            error: function (data) {
                showToast('网络忙，请重试');
            },
            complete: function () {
            }
        });    
    }
    
    function drawfk(todayfk,hisfk,fkPoint)
    {
      	var temp={
                data:[],
                time:[]
            };
      	var datalist=null;
 	
       	if(todayfk.items.length<fkPoint && hisfk!=null && hisfk.items!=undefined && hisfk.items.length>1)
    	{
    		var pos=(hisfk.items.length-1)-(fkPoint-todayfk.items.length)+1; //hisfs[0]为数据头 hisfs[1]开始才为明细数据
    		if(pos<1) pos=1; //hisfk5[0]不是数据明细
    		datalist=hisfk.items.slice(pos).concat(todayfk.items);
    	}
    	else
    	{
    		var pos=todayfk.items.length-fkPoint;
    		if(pos<0) pos=0;
    		datalist=todayfk.items.slice(pos);
    	}
     	
    	//console.log(datalist);
    	if(datalist!=null)
    	{
       		var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/ ;
    		for(var i = 0; i < datalist.length; i++){
    			temp.data.push([datalist[i].o,datalist[i].c,datalist[i].l,datalist[i].h]);//open,close,low,high
    			var t=datalist[i].t;
    			var formatedDate = t.replace(pattern, '$1-$2-$3 $4:$5');
    			var d=new Date(formatedDate.replace(/-/g, "/"));
    			temp.time.push(d.format('h:mm'));
    		}
    	}
    	//console.log("合并后分K");
    	//console.log(temp);
    	renderData(todayfk.period,temp);
    	
    	
    }
    
    /** 历史日K线 **/
    function getHisdayfk(hisfkday)
    {
    	var bidVal=hisfkday.bid,widVal=hisfkday.wid;
    	var countVal="30"; //过去的30个交易日
    	var periodVal="D"; //D：日   W：周    M:月
    	$.ajax({
            url: "/api/v1/market/kline",
            data: {bid:bidVal,wid:widVal,count:countVal,period:periodVal},
            type: 'post',
            dataType: 'json',
            contentType:"application/x-www-form-urlencoded;charset=utf-8",
            beforeSend: function () {
            },
            success: function (vdata) {
            	//console.log("原始日K线数据");
            	//console.log(vdata);
              	getTodaydayfk(vdata);
             	
             },
            error: function (data) {
                showToast('网络忙，请重试');
            },
            complete: function () {
            }
        });    
    }
    
   function drawDayk(hisfkday)
   {
	   var temp={
               data:[],
               time:[]
           };
   	
	 	if(hisfkday!=null && hisfkday.items!=undefined && hisfkday.items.length>0)
	 	{
	    	var pattern = /(\d{4})(\d{2})(\d{2})/ ;
	 		for(var i = 0; i < hisfkday.items.length; i++){
   			temp.data.push([hisfkday.items[i].o,hisfkday.items[i].c,hisfkday.items[i].l,hisfkday.items[i].h]);//open,close,low,high
   			var t=hisfkday.items[i].d;
   			var formatedDate = t.replace(pattern, '$1-$2-$3');
   			temp.time.push(new Date(formatedDate).format('MM-dd'));
	 		}
	 	}
	 	//console.log(temp);
	 	renderData(100,temp);
   }
    
    function nowtime(){//将当前时间转换成yyyymmdd格式
        var mydate = new Date();
        var str = "" + mydate.getFullYear();
        var mm = mydate.getMonth()+1
        if(mydate.getMonth()>9){
         str += mm;
        }
        else{
         str += "0" + mm;
        }
        if(mydate.getDate()>9){
         str += mydate.getDate();
        }
        else{
         str += "0" + mydate.getDate();
        }
        return str;
    }
    
    /** 当天日K线 **/
    function getTodaydayfk(hisfkday)
    {
    	//console.log("rrrrrr");
    	//console.log(quotes);
    	var obj={};
    	$.each(quotes, function(index, objVal) {
    		if(hisfkday.items==null )
    		{
        	    hisfkday.items=[];
    		}
    		if(objVal.bid==cfg.bid && objVal.wid==cfg.wid && objVal.o*1.0!=0 && objVal.c*1.0!=0)
    		{
				
				obj.h=objVal.h;
				obj.l=objVal.l;
				obj.o=objVal.o;
				obj.c=objVal.c;
				obj.d=objVal.d;
				obj.t=objVal.t;
				if(obj.t*1.0>=0 && obj.t*1.0<=040000)
				{
					obj.d=hisfkday.items[hisfkday.items.length-1].d;
				}
     		}
    	});
    	//console.log("今天k线");
    	//console.log(obj);
    	var temp={
                data:[],
                time:[]
            };
    	
    	if(obj.h!=undefined )
    	{
    		hisfkday.items=hisfkday.items.concat(obj);
    	}
    	//console.log("合并后k线");
    	//console.log(hisfkday);
	 	if(hisfkday!=null && hisfkday.items!=undefined && hisfkday.items.length>0)
	 	{
	    	var pattern = /(\d{4})(\d{2})(\d{2})/ ;
	 		for(var i = 0; i < hisfkday.items.length; i++){
    			temp.data.push([hisfkday.items[i].o,hisfkday.items[i].c,hisfkday.items[i].l,hisfkday.items[i].h]);//open,close,low,high
    			var t=hisfkday.items[i].d;
    			var formatedDate = t.replace(pattern, '$1-$2-$3');
    			temp.time.push(new Date(formatedDate).format('MM-dd'));
	 		}
	 	}
	 	//console.log(temp);
	 	renderData(100,temp);
	 	
    }    
    
    
    
    /**
     * 整合标题与内容分散的数据为一体
     * @param {StringArray} nameList 标题数组
     * @param {AnyArray} valueList 内容数组
     */
    var transferList = (function(){
    	var map = function(nameList, values){
    		if(nameList.length > values.length){
    			var error = new Error("Name list length: " + nameList.length + " does not values length: " + values.length);
    			console.error(error, nameList, values);
    			throw error;
    		}

    		var obj = {};
    		for(var i = 0; i < nameList.length; i++){
    			var name = nameList[i], value = values[i];
    			obj[name] = value;
    		}

    		return obj;
    	};

    	return function(nameFsTitleList,nameFsList, valueList){
    		var tmp={};
    		var arr = [];
    		var i=0;
    		valueList.forEach(function(values){
    			if(null == values)
    				return;
    			var obj=null;
    			if(i==0 && nameFsTitleList!=null)
    			{
    				var obj = map(nameFsTitleList, values);
    				tmp.wid=obj.wid;
    				tmp.bid=obj.bid;
    				if(obj.k!=undefined)
    					tmp.k=obj.k;
    			}
    			else
    			{
    				var obj = map(nameFsList, values);
    				if(obj!=null)
        				arr.push(obj);
    			}
    			
    			i=i+1;
    		});
    		tmp.items=arr;
    		return tmp;
    	};
    })();
    